package pl.register.android.preferences;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import pl.register.android.course.Course;
import pl.register.android.group.GroupType;
import pl.register.android.lesson.TagLesson;
import pl.register.android.year.YearType;

/**
 * @author Mateusz Strzelecki
 * @date 03.11.12
 */

public class ResotorePreferences {
	private static final String TAG = ResotorePreferences.class.getName();

	private static final String KEY_START_YEAR_COURSE = "restoreStartYearCourse";
	private static final String KEY_END_YEAR_COURSE = "restoreEndYearCourse";
	private static final String KEY_YEAR_TYPE = "restoreYearType";
	private static final String KEY_CORRELATION_YEAR_TYPE = "restoreCorrelationYearType";
	private static final String KEY_CORRELATION_GROUP_TYPE = "restoreCorrelationGroupType";
	private static final String KEY_TAG = "restoreTag";

	private static final String DEFAULT_TAG = TagLesson.OCTOBER.toString();
	private static final String DEFAULT_YEAR_TYPE = YearType.FIRST_YEAR.toString();
	private static final String DEFAULT_GROUP_TYPE = GroupType.MORNING.toString();

	public ResotorePreferences() {
		Log.d(TAG, "rejestracja");
	}

	public static void saveYearType(Context context, YearType yearType) {
		SharedPreferences preferences = getPreferences(context);

		Log.d(TAG, "Zapis typu roku " + yearType);

		preferences.edit()
				.putString(KEY_YEAR_TYPE, yearType.toString())
				.commit();
	}

    public static void saveTag(Context context, TagLesson tag) {
        SharedPreferences preferences = getPreferences(context);

        Log.d(TAG, "Zapis tag'a " + tag);

        preferences.edit()
                .putString(KEY_TAG, tag.toString())
                .commit();
    }

    public static void saveCourse(Context context, Course course) {
        SharedPreferences preferences = getPreferences(context);

        Log.d(TAG, "Zapis kursu" + course);

        preferences.edit()
                .putInt(KEY_START_YEAR_COURSE, course.getStartYear())
                .putInt(KEY_END_YEAR_COURSE, course.getEndYear())
                .commit();
    }

    public static TagLesson getTag(Context ctx) {
        SharedPreferences preferences = getPreferences(ctx);
        String tagString = preferences.getString(KEY_TAG, DEFAULT_TAG);

        TagLesson tag = TagLesson.valueOf(tagString);
        Log.d(TAG, "Pobieranie tagu " + tag);
        return tag;
    }

    public static void saveCorrelationYearType(Context context, YearType yearType) {
        SharedPreferences preferences = getPreferences(context);

        Log.d(TAG, "Zapis typ roku, zestawienie " + yearType);

        preferences.edit()
                .putString(KEY_CORRELATION_YEAR_TYPE, yearType.toString())
                .commit();
    }

    public static void saveCorrelationGroupType(Context context, GroupType groupType) {
        SharedPreferences preferences = getPreferences(context);

        Log.d(TAG, "Zapis typ grupy, zestawienie " + groupType);

        preferences.edit()
                .putString(KEY_CORRELATION_GROUP_TYPE, groupType.toString())
                .commit();
    }

    public static YearType getCorrelationYearType(Context ctx) {
        SharedPreferences preferences = getPreferences(ctx);
        String yearTypeString = preferences.getString(KEY_CORRELATION_YEAR_TYPE, DEFAULT_YEAR_TYPE);

        YearType yearType = YearType.valueOf(yearTypeString);
        Log.d(TAG, "Pobieranie kursu " + yearType);
        return yearType;
    }

    public static GroupType getCorrelationGroupType(Context ctx) {
        SharedPreferences preferences = getPreferences(ctx);
        String groupTypeString = preferences.getString(KEY_CORRELATION_GROUP_TYPE, DEFAULT_GROUP_TYPE);

        GroupType groupType = GroupType.valueOf(groupTypeString);
        Log.d(TAG, "Pobieranie kursu " + groupType);
        return groupType;
    }

    public static YearType getYearType(Context ctx) {
        SharedPreferences preferences = getPreferences(ctx);
        String yearTypeString = preferences.getString(KEY_YEAR_TYPE, DEFAULT_YEAR_TYPE);

        YearType yearType = YearType.valueOf(yearTypeString);
        Log.d(TAG, "Pobieranie kursu " + yearType);
        return yearType;
    }

    public static Course getCourse(Context ctx) {
		SharedPreferences preferences = getPreferences(ctx);
		int startYear = preferences.getInt(KEY_START_YEAR_COURSE, 0);
		int endYear = preferences.getInt(KEY_END_YEAR_COURSE, 0);

        Course course = new Course(startYear, endYear);
		Log.d(TAG, "Pobieranie kursu " + course);
		return course;
	}

	public static void flush(Context ctx) {
		SharedPreferences preferences = getPreferences(ctx);
		preferences.edit()
				.remove(KEY_START_YEAR_COURSE)
				.remove(KEY_END_YEAR_COURSE)
				.remove(KEY_YEAR_TYPE)
				.remove(KEY_TAG)
				.remove(KEY_CORRELATION_GROUP_TYPE)
				.remove(KEY_CORRELATION_YEAR_TYPE)
				.commit();
	}

	private static SharedPreferences getPreferences(Context context) {
		return PreferenceManager.getDefaultSharedPreferences(context);
	}
}